package com.ruoyi.project.system.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.domain.TreeSelect;
import com.ruoyi.project.system.domain.SysDept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.mapper.SysCredentialsTypeMapper;
import com.ruoyi.project.system.domain.SysCredentialsType;
import com.ruoyi.project.system.service.ISysCredentialsTypeService;

/**
 * 证书类型Service业务层处理
 *
 * @author ruoyi
 * @date 2020-04-26
 */
@Service
public class SysCredentialsTypeServiceImpl implements ISysCredentialsTypeService {
    @Autowired
    private SysCredentialsTypeMapper sysCredentialsTypeMapper;

    /**
     * 查询证书类型
     *
     * @param credentialsTypeId 证书类型ID
     * @return 证书类型
     */
    @Override
    public SysCredentialsType selectSysCredentialsTypeById(Long credentialsTypeId) {
        return sysCredentialsTypeMapper.selectSysCredentialsTypeById(credentialsTypeId);
    }

    /**
     * 查询证书类型列表
     *
     * @param sysCredentialsType 证书类型
     * @return 证书类型
     */
    @Override
    public List<SysCredentialsType> selectSysCredentialsTypeList(SysCredentialsType sysCredentialsType) {
        return sysCredentialsTypeMapper.selectSysCredentialsTypeList(sysCredentialsType);
    }

    /**
     * 新增证书类型
     *
     * @param sysCredentialsType 证书类型
     * @return 结果
     */
    @Override
    public int insertSysCredentialsType(SysCredentialsType sysCredentialsType) {
        SysCredentialsType info = sysCredentialsTypeMapper.selectSysCredentialsTypeById(sysCredentialsType.getParentId());
        // 如果父节点不为正常状态,则不允许新增子节点
        if (!UserConstants.DEPT_NORMAL.equals(info.getStatus())) {
            throw new CustomException("类型停用，不允许新增");
        }
        sysCredentialsType.setAncestors(info.getAncestors() + "," + sysCredentialsType.getParentId());
        sysCredentialsType.setCreateTime(DateUtils.getNowDate());
        return sysCredentialsTypeMapper.insertSysCredentialsType(sysCredentialsType);
    }

    /**
     * 修改证书类型
     *
     * @param sysCredentialsType 证书类型
     * @return 结果
     */
    @Override
    public int updateSysCredentialsType(SysCredentialsType sysCredentialsType) {
        sysCredentialsType.setUpdateTime(DateUtils.getNowDate());
        return sysCredentialsTypeMapper.updateSysCredentialsType(sysCredentialsType);
    }

    /**
     * 批量删除证书类型
     *
     * @param credentialsTypeIds 需要删除的证书类型ID
     * @return 结果
     */
    @Override
    public int deleteSysCredentialsTypeByIds(Long[] credentialsTypeIds) {
        return sysCredentialsTypeMapper.deleteSysCredentialsTypeByIds(credentialsTypeIds);
    }

    /**
     * 删除证书类型信息
     *
     * @param credentialsTypeId 证书类型ID
     * @return 结果
     */
    @Override
    public int deleteSysCredentialsTypeById(Long credentialsTypeId) {
        return sysCredentialsTypeMapper.deleteSysCredentialsTypeById(credentialsTypeId);
    }

    /**
     * 校验证书类型名称是否唯一
     *
     * @param credentialsType
     * @return
     */
    @Override
    public String checkTypeNameUnique(SysCredentialsType credentialsType) {
        Long credentialsTypeId = StringUtils.isNull(credentialsType.getCredentialsTypeId()) ? -1L : credentialsType.getCredentialsTypeId();
        SysCredentialsType info = sysCredentialsTypeMapper.checkTypeNameUnique(credentialsType.getCredentialsType(), credentialsType.getParentId());
        if (StringUtils.isNotNull(info) && info.getCredentialsTypeId().longValue() != credentialsTypeId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }


    @Override
    public List<TreeSelect> buildDeptTreeSelect(List<SysCredentialsType> depts) {
        List<SysCredentialsType> deptTrees = buildDeptTree(depts);
        return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    @Override
    public List<SysCredentialsType> buildDeptTree(List<SysCredentialsType> credentialsTypes) { {
            List<SysCredentialsType> returnList = new ArrayList<SysCredentialsType>();
            List<Long> tempList = new ArrayList<Long>();
            for (SysCredentialsType dept : credentialsTypes) {
                tempList.add(dept.getCredentialsTypeId());
            }
            for (Iterator<SysCredentialsType> iterator = credentialsTypes.iterator(); iterator.hasNext(); ) {
                SysCredentialsType dept = (SysCredentialsType) iterator.next();
                // 如果是顶级节点, 遍历该父节点的所有子节点
                if (!tempList.contains(dept.getParentId())) {
                    recursionFn(credentialsTypes, dept);
                    returnList.add(dept);
                }
            }
            if (returnList.isEmpty()) {
                returnList = credentialsTypes;
            }
            return returnList;
        }
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<SysCredentialsType> list, SysCredentialsType t) {
        // 得到子节点列表
        List<SysCredentialsType> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SysCredentialsType tChild : childList) {
            if (hasChild(list, tChild)) {
                // 判断是否有子节点
                Iterator<SysCredentialsType> it = childList.iterator();
                while (it.hasNext()) {
                    SysCredentialsType n = (SysCredentialsType) it.next();
                    recursionFn(list, n);
                }
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<SysCredentialsType> getChildList(List<SysCredentialsType> list, SysCredentialsType t) {
        List<SysCredentialsType> tlist = new ArrayList<SysCredentialsType>();
        Iterator<SysCredentialsType> it = list.iterator();
        while (it.hasNext()) {
            SysCredentialsType n = (SysCredentialsType) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getCredentialsTypeId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SysCredentialsType> list, SysCredentialsType t) {
        return getChildList(list, t).size() > 0 ? true : false;
    }
}
